Ext.define('Monkey.view.code.GeneratorController', {
    extend: 'Monkey.view.BaseController',
    alias: 'controller.ctrl-generator',

    init: function () {

    },

    showPropertiesWindow: function () {
        var window = Ext.create('Monkey.view.code.PropertiesWindow');
        window.show();
    },

    cancelWindow: function () {
        this.getView().close();
    },

    showPicture: function (cmp) {
        var record = cmp.getWidgetRecord();
        if (Ext.isEmpty(record.data.picture)) {
            Ext.Msg.show({
                title: '提示',
                message: '没有预览图片',
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.INFO
            });
            return;
        }
        var pictureWindow = Ext.create('Monkey.view.component.PictureWindow', {
            src: Monkey.URL.RESOURCE_FULL_HOST + record.data.picture
        });
        pictureWindow.show();
    },

    addProperty: function () {
        var form = this.getView().down('form');
        var store = Ext.getCmp('modelPropertyGrid').getStore();
        store.add({
            name: form.getComponent('name').getValue(),
            type: form.getComponent('type').getValue(),
            remark: form.getComponent('remark').getValue()
        });
        this.cancelWindow();
    },

    removeProperty: function () {
        var modelPropertyGrid = this.getReferences().modelPropertyGrid;
        var records = modelPropertyGrid.getSelectionModel().getSelection();
        if (records.length < 1) {
            Ext.Msg.show({
                title: '提示',
                message: '请先选择一行数据',
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
            return;
        }
        var store = modelPropertyGrid.getStore();
        store.remove(records);
    },

    selectTemplate: function (o, record, tr, rowIndex, e, eOpts) {
        var customParamConfig = this.getReferences().customParamConfig;
        customParamConfig.removeAll();
        var cmp = Ext.create("Monkey.view." + record.data.view);
        customParamConfig.add(cmp);
    },

    startGenerate: function () {
        var me = this,
            references = this.getReferences(),
            modelParamConfig = references.modelParamConfig,
            templateGrid = references.templateGrid,
            checkParamConfig = references.checkParamConfig,
            customParamConfig = references.customParamConfig,
            modelPropertyGrid = references.modelPropertyGrid;

        if (!modelParamConfig.isValid()) {
            return;
        }
        var propertiesStore = modelPropertyGrid.getStore();
        if (propertiesStore.getCount() < 1) {
            Ext.Msg.show({
                title: '提示',
                message: '实体类至少有一个属性',
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
            return;
        }
        var modelValues = modelParamConfig.getValues();
        var checkValues = checkParamConfig.getValues();
        var selectTemplates = templateGrid.getSelectionModel().getSelection();
        if (checkValues.isView == 'on' && selectTemplates.length < 1) {
            Ext.Msg.show({
                title: '提示',
                message: '选择生成View就必须选择一个模版',
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
            return;
        }

        var customForm = customParamConfig.down('form');
        var result = customForm.validate();
        if (!result.success) {
            Ext.Msg.show({
                title: '提示',
                message: result.message,
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
            return;
        }
        var customValues = customForm.getValues();
        var properties = [];
        propertiesStore.each(function (record) {
            properties.push(record.data);
        });

        this.request(Monkey.URL.SERVER_URL + '/m/code/generate.json', "POST", {
            modelParam: Ext.encode(modelValues),
            properties: Ext.encode(properties),
            files: Ext.encode(checkValues),
            template: Ext.encode(selectTemplates[0].data),
            customValues: Ext.encode(customValues)
        }, function (data) {
            console.log(data);
            var icon = Ext.Msg.ERROR;
            if (data.success) {
                icon = Ext.Msg.INFO;
            }
            Ext.Msg.show({
                title: '提示',
                message: data.message,
                buttons: Ext.Msg.OK,
                icon: icon
            });
        });

    }

});
